package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.gson.Gson;
import com.psa.tool.*;

/**
 * Created by david on 2014-06-10.
 */
public class Brand implements IJson,ISqlObject {
    public String ID = "";
    public String ChineseName = "";
    public String EnglishName = "";

    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("ID", ID.trim());
        values.put("ChineseName", ChineseName.trim());
        values.put("EnglishName", EnglishName.trim());
        return (int)dbTool.insert("Brand", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("ChineseName", ChineseName.trim());
        values.put("EnglishName", EnglishName.trim());
        String where = "ID = ?";
        String[] whereval = { ID.trim() };
        return dbTool.update("Brand", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "ID = ?";
        String[] wherevalue = { ID.trim() };
        return dbTool.delete("Brand", where, wherevalue);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            Brand brand = new Brand();
            brand.ID = cursor.getString(0);
            brand.ChineseName = cursor.getString(1);
            brand.EnglishName = cursor.getString(2);
            return brand;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("Brand", "ID Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "Brand";
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }

    @Override
    public String toString() {
        return ChineseName ;
    }
}
